package com.zjf.carpark.controller;


import com.zjf.carpark.entities.Owner;
import com.zjf.carpark.service.impl.OwnerServiceImpl;
import com.zjf.carpark.utils.ResponseData;
import com.zjf.carpark.vo.OwnerLoginVO;
import com.zjf.carpark.vo.PhoneLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.zjf.carpark.utils.CodeAndMessage.*;
import static com.zjf.carpark.utils.SendSmsUtil.sendSms;

/**
 * @description:
 * @author: 张建飞
 * @createDate: 2020/2/4
 * @version: 1.0
 */
@Controller
@CrossOrigin
@RequestMapping("weiXin")
public class WXLoginController {

    @Autowired
    OwnerServiceImpl ownerService;

    ResponseData responseData = new ResponseData(CODE0, ERROR);

    /**
     * 停车用户登录
     *
     * @param request
     * @param response
     * @param ownerLoginVO
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public ResponseData login(HttpServletRequest request, HttpServletResponse response, OwnerLoginVO ownerLoginVO) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        System.out.println(ownerLoginVO.getUsername());
        System.out.println(ownerLoginVO.getPassword());
        String randomcode_key = (String) request.getSession().getAttribute("randomcode_key");
        Owner login = ownerService.login(ownerLoginVO.getUsername(), ownerLoginVO.getPassword());

        if (login != null) {
            if (ownerLoginVO.getCode() != null && ownerLoginVO.getCode().equalsIgnoreCase(randomcode_key)) {
                if (login.getFlag() != 0) {
                    request.getSession().setAttribute("owner", login);
                    responseData = new ResponseData(CODE200, SUCCESS);
                    responseData.putDataValue("owner", login);
                } else {
                    responseData = new ResponseData(808, "用户暂时不可用");
                }
            } else {
                responseData = new ResponseData(CHECKCODE, CHECKCODEERROR);
            }
        }
        return responseData;
    }

    /**
     * 发送验证码
     *
     * @param phone
     * @return
     */
    @RequestMapping("getCode")
    @ResponseBody
    public ResponseData getCode(HttpServletRequest request, String phone) {

        int code = (int) ((Math.random() * 9 + 1) * 100000);
        System.out.println(code);
        request.getSession().setAttribute("phoneCode", code);
        String s = sendSms(phone, code);
        if (s != null && "OK".equals(s.substring(12, 14))) {
            responseData = new ResponseData(CODE200, SUCCESS);
        }
        return responseData;
    }

    @RequestMapping("phoneLogin")
    @ResponseBody
    public ResponseData phoneLogin(HttpServletRequest request, PhoneLoginVO phoneLoginVO) {
        int phoneCode = (int) request.getSession().getAttribute("phoneCode");
        System.out.println(phoneCode);
        System.out.println(phoneLoginVO.getCode());
        if (phoneLoginVO.getCode() == phoneCode) {
            Owner owner = ownerService.phoneLogin(phoneLoginVO.getOwnerTel());
            if (owner != null) {
                responseData = new ResponseData(CODE200, SUCCESS);
                responseData.putDataValue("owner", owner);
            } else {
                responseData = new ResponseData(808, "此号码没有绑定车位，请联系管理员");
            }
        }
        return responseData;
    }


}
